#pragma once

//对互斥锁进行封装
#include<iostream>
#include<pthread.h>
#include<unistd.h>

class Mutex
{
public:
    Mutex()
    {
        //初始化工作
        pthread_mutex_init(&_lock , nullptr);
    }
    void Lock()
    {
        pthread_mutex_lock(&_lock);
    }
    void Unlock()
    {
        pthread_mutex_unlock(&_lock);
    }
    //给条件标量提供获取锁
    pthread_mutex_t* Get()
    {
        return &_lock;
    }
    ~Mutex()
    {
        //销毁
        pthread_mutex_destroy(&_lock);
    }
private:
    pthread_mutex_t _lock;//锁
};

class LockGuard
{
public:
    LockGuard(Mutex* mutex):_mutexp(mutex)
    {
        // 构造的时候上锁
        _mutexp->Lock();
    }
    ~LockGuard()
    {
        //析构的时候解锁
        _mutexp->Unlock();
    }
private:
    Mutex* _mutexp;
};